package model

import (
	"awesomeProject2/global"
)

type NumberInfo struct {
	Paper       int64 `json:"paper"`
	Project     int64 `json:"project"`
	Achievement int64 `json:"achievement"`
	Work        int64 `json:"work"`
	Patent      int64 `json:"patent"`
}

//返回数据库条数

func GetAllNumber(year int) NumberInfo {
	_, papernumber := GetAllpp(year)
	_, projectnumber := GetAllpj(year)
	_, achievementnumber := GetAllac(year)
	_, worknumber := GetAllwk(year)
	_, patentnumber := GetAllpt(year)
	var data NumberInfo
	data = NumberInfo{
		Paper:       papernumber,
		Project:     projectnumber,
		Achievement: achievementnumber,
		Work:        worknumber,
		Patent:      patentnumber,
	}
	return data
}

type PaperInfo struct {
	IA int64 `json:"ia"`
	CA int64 `json:"ca"`
	CB int64 `json:"cb"`
	S1 int64 `json:"s1"`
	S2 int64 `json:"s2"`
}

func GetPaperDetail(year int) PaperInfo {
	var data PaperInfo

	var ia_number int64
	var ca1_number int64
	var ca2_number int64
	var cb1_number int64
	var cb2_number int64
	var s1 int64
	var s2 int64
	global.GVA_DB.Model(&PaperToLevel{}).Where("level_id = ? AND year = ?", 1, year).Count(&ia_number)
	global.GVA_DB.Model(&PaperToLevel{}).Where("level_id = ? AND year = ?", 5, year).Count(&ca1_number)
	global.GVA_DB.Model(&PaperToLevel{}).Where("level_id = ? AND year = ?", 12, year).Count(&ca2_number)
	ca_number := ca1_number + ca2_number
	global.GVA_DB.Model(&PaperToLevel{}).Where("level_id = ? AND year = ?", 6, year).Count(&cb1_number)
	global.GVA_DB.Model(&PaperToLevel{}).Where("level_id = ? AND year = ?", 13, year).Count(&cb2_number)
	cb_number := cb1_number + cb2_number
	global.GVA_DB.Model(&PaperToLevel{}).Where("level_id = ? AND year = ?", 2, year).Count(&s1)
	global.GVA_DB.Model(&PaperToLevel{}).Where("level_id = ? AND year = ?", 3, year).Count(&s2)
	data = PaperInfo{
		IA: ia_number,
		CA: ca_number,
		CB: cb_number,
		S1: s1,
		S2: s2,
	}
	return data
}

type PatentInfo struct {
	A int64 `json:"a"`
	B int64 `json:"b"`
	C int64 `json:"c"`
}

func GetPatentDetail(year int) PatentInfo {
	var data PatentInfo
	var a_number int64
	var b_number int64
	var c_number int64
	global.GVA_DB.Model(&Patent{}).Where("patent_category = ? AND year = ?", "发明专利", year).Count(&a_number)
	global.GVA_DB.Model(&Patent{}).Where("patent_category = ? AND year = ?", "实用新型", year).Count(&b_number)
	global.GVA_DB.Model(&Patent{}).Where("patent_category = ? AND year = ?", "外观设计", year).Count(&c_number)
	data = PatentInfo{
		A: a_number,
		B: a_number,
		C: c_number,
	}
	return data
}

type AcInfo struct {
	A int64 `json:"a"`
	B int64 `json:"b"`
	C int64 `json:"c"`
	D int64 `json:"d"`
	E int64 `json:"e"`
	F int64 `json:"f"`
	G int64 `json:"g"`
	H int64 `json:"h"`
	I int64 `json:"i"`
}

func GetAcDetail(year int) AcInfo {
	var a, b, c, d, e, f, g, h, i int64
	var data AcInfo
	global.GVA_DB.Model(&Achievement{}).Where("achievement_category = ? AND achievement_level = ?  AND year = ?  ", "国家级", "一等奖", year).Count(&a)
	global.GVA_DB.Model(&Achievement{}).Where("achievement_category = ? AND achievement_level = ?  AND year = ?  ", "国家级", "二等奖", year).Count(&b)
	global.GVA_DB.Model(&Achievement{}).Where("achievement_category = ? AND achievement_level = ?  AND year = ?  ", "国家级", "三等奖", year).Count(&c)
	global.GVA_DB.Model(&Achievement{}).Where("achievement_category = ? AND achievement_level = ?  AND year = ?  ", "省部级", "一等奖", year).Count(&d)
	global.GVA_DB.Model(&Achievement{}).Where("achievement_category = ? AND achievement_level = ?  AND year = ?  ", "省部级", "二等奖", year).Count(&e)
	global.GVA_DB.Model(&Achievement{}).Where("achievement_category = ? AND achievement_level = ?  AND year = ?  ", "省部级", "三等奖", year).Count(&f)
	global.GVA_DB.Model(&Achievement{}).Where("achievement_category = ? AND achievement_level = ?  AND year = ?  ", "厅局级", "一等奖", year).Count(&g)
	global.GVA_DB.Model(&Achievement{}).Where("achievement_category = ? AND achievement_level = ?  AND year = ?  ", "厅局级", "二等奖", year).Count(&h)
	global.GVA_DB.Model(&Achievement{}).Where("achievement_category = ? AND achievement_level = ?  AND year = ?  ", "厅局级", "三等奖", year).Count(&i)
	data = AcInfo{
		A: a,
		B: b,
		C: c,
		D: d,
		E: e,
		F: f,
		G: g,
		H: h,
		I: i,
	}
	return data
}

type PJInfo struct {
	A int64 `json:"a"`
	B int64 `json:"b"`
	C int64 `json:"c"`
	D int64 `json:"d"`
}

func GetPJDetail(year int) PJInfo {
	var a, b, c, d int64
	var data PJInfo
	global.GVA_DB.Model(&Project{}).Where("project_level = ?  AND year = ?  ", "国家级", year).Count(&a)
	global.GVA_DB.Model(&Project{}).Where("project_level = ?  AND year = ?  ", "国家重点", year).Count(&b)
	global.GVA_DB.Model(&Project{}).Where("project_level = ?  AND year = ?  ", "省部级", year).Count(&c)
	global.GVA_DB.Model(&Project{}).Where("project_level = ?  AND year = ?  ", "企业", year).Count(&d)
	data = PJInfo{
		A: a,
		B: b,
		C: c,
		D: d,
	}
	return data
}

type WorkInfo struct {
	A int64 `json:"a"`
	B int64 `json:"b"`
}

func GetWKDetail(year int) WorkInfo {
	var a, b int64
	var data WorkInfo
	global.GVA_DB.Model(&Work{}).Where("work_level = ? AND year = ?  ", "国家级", year).Count(&a)
	global.GVA_DB.Model(&Work{}).Where("work_level = ? AND year = ?  ", "省部级", year).Count(&b)
	data = WorkInfo{
		A: a,
		B: b,
	}
	return data
}

type WorkA struct {
	A int64 `json:"a"`
	B int64 `json:"b"`
	C int64 `json:"c"`
	D int64 `json:"d"`
	E int64 `json:"e"`
}

func GetFiveYearsWks(year int) WorkA {
	var a, b, c, d, e int64

	var data WorkA
	global.GVA_DB.Model(&Work{}).Where("year = ?", year).Count(&a)
	global.GVA_DB.Model(&Work{}).Where("year = ?", year-1).Count(&b)
	global.GVA_DB.Model(&Work{}).Where("year = ?", year-2).Count(&c)
	global.GVA_DB.Model(&Work{}).Where("year = ?", year-3).Count(&d)
	global.GVA_DB.Model(&Work{}).Where("year = ?", year-4).Count(&e)
	data = WorkA{
		A: a,
		B: b,
		C: c,
		D: d,
		E: e,
	}
	return data
}

func GetPersonACMPaperInfo(userid uint) []int64 {
	var a, b, c, d, e, f int64
	var data []int64
	Year := GetYear()
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 1, Year).Count(&a)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 1, Year-1).Count(&b)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 1, Year-2).Count(&c)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 1, Year-3).Count(&d)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 1, Year-4).Count(&e)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 1, Year-5).Count(&f)
	data = []int64{f, e, d, c, b, a}
	return data
}
func GetPersonCCFPaperInfo(userid uint) []int64 {
	var a, b, c, d, e, f int64
	var a2, b2, c2, d2, e2, f2 int64
	var data []int64

	Year := GetYear()
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 5, Year).Count(&a)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 5, Year-1).Count(&b)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 5, Year-2).Count(&c)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 5, Year-3).Count(&d)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 5, Year-4).Count(&e)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 5, Year-5).Count(&f)

	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 12, Year).Count(&a2)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 12, Year-1).Count(&b2)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 12, Year-2).Count(&c2)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 12, Year-3).Count(&d2)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 12, Year-4).Count(&e2)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 12, Year-5).Count(&f2)

	data = []int64{f + f2, e + e2, d + d2, c + c2, b + b2, a + a2}
	return data
}
func GetPersonSCIPaperInfo(userid uint) []int64 {
	var a, b, c, d, e, f int64
	var data []int64
	Year := GetYear()
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 2, Year).Count(&a)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 2, Year-1).Count(&b)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 2, Year-2).Count(&c)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 2, Year-3).Count(&d)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 2, Year-4).Count(&e)
	global.GVA_DB.Model(&PaperToLevel{}).Where("user_id = ?", userid).Where("level_id = ? AND year = ?", 2, Year-5).Count(&f)
	data = []int64{f, e, d, c, b, a}
	return data
}
func GetPersonProject(userid uint, level string) []int64 {
	var a, b, c, d, e, f int64
	var data []int64
	Year := GetYear()
	global.GVA_DB.Model(&Project{}).Where("user_id = ?", userid).Where("project_level = ?  AND year = ?  ", level, Year).Count(&a)
	global.GVA_DB.Model(&Project{}).Where("user_id = ?", userid).Where("project_level = ?  AND year = ?  ", level, Year-1).Count(&b)
	global.GVA_DB.Model(&Project{}).Where("user_id = ?", userid).Where("project_level = ?  AND year = ?  ", level, Year-2).Count(&c)
	global.GVA_DB.Model(&Project{}).Where("user_id = ?", userid).Where("project_level = ?  AND year = ?  ", level, Year-3).Count(&d)
	global.GVA_DB.Model(&Project{}).Where("user_id = ?", userid).Where("project_level = ?  AND year = ?  ", level, Year-4).Count(&e)
	global.GVA_DB.Model(&Project{}).Where("user_id = ?", userid).Where("project_level = ?  AND year = ?  ", level, Year-5).Count(&f)
	data = []int64{f, e, d, c, b, a}
	return data
}
func GetPersonWk(userid uint, level string) []int64 {
	var a, b, c, d, e, f int64
	var data []int64
	Year := GetYear()
	global.GVA_DB.Model(&Work{}).Where("user_id = ?", userid).Where("work_level = ?  AND year = ?  ", level, Year).Count(&a)
	global.GVA_DB.Model(&Work{}).Where("user_id = ?", userid).Where("work_level = ?  AND year = ?  ", level, Year-1).Count(&b)
	global.GVA_DB.Model(&Work{}).Where("user_id = ?", userid).Where("work_level = ?  AND year = ?  ", level, Year-2).Count(&c)
	global.GVA_DB.Model(&Work{}).Where("user_id = ?", userid).Where("work_level = ?  AND year = ?  ", level, Year-3).Count(&d)
	global.GVA_DB.Model(&Work{}).Where("user_id = ?", userid).Where("work_level = ?  AND year = ?  ", level, Year-4).Count(&e)
	global.GVA_DB.Model(&Work{}).Where("user_id = ?", userid).Where("work_level = ?  AND year = ?  ", level, Year-5).Count(&f)
	data = []int64{f, e, d, c, b, a}
	return data
}
func GetPersonAC(userid uint, level string) []int64 {
	var a, b, c, d, e, f int64
	var data []int64
	Year := GetYear()
	global.GVA_DB.Model(&Achievement{}).Where("user_id = ?", userid).Where("achievement_category= ?  AND year = ?  ", level, Year).Count(&a)
	global.GVA_DB.Model(&Achievement{}).Where("user_id = ?", userid).Where("achievement_category = ?  AND year = ?  ", level, Year-1).Count(&b)
	global.GVA_DB.Model(&Achievement{}).Where("user_id = ?", userid).Where("achievement_category= ?  AND year = ?  ", level, Year-2).Count(&c)
	global.GVA_DB.Model(&Achievement{}).Where("user_id = ?", userid).Where("achievement_category = ?  AND year = ?  ", level, Year-3).Count(&d)
	global.GVA_DB.Model(&Achievement{}).Where("user_id = ?", userid).Where("achievement_category = ?  AND year = ?  ", level, Year-4).Count(&e)
	global.GVA_DB.Model(&Achievement{}).Where("user_id = ?", userid).Where("achievement_category= ?  AND year = ?  ", level, Year-5).Count(&f)
	data = []int64{f, e, d, c, b, a}
	return data
}
func GetPersonPT(userid uint, level string) []int64 {
	var a, b, c, d, e, f int64
	var data []int64
	Year := GetYear()
	global.GVA_DB.Model(&Patent{}).Where("user_id = ?", userid).Where("patent_category = ?  AND year = ?  ", level, Year).Count(&a)
	global.GVA_DB.Model(&Patent{}).Where("user_id = ?", userid).Where("patent_category = ?  AND year = ?  ", level, Year-1).Count(&b)
	global.GVA_DB.Model(&Patent{}).Where("user_id = ?", userid).Where("patent_category = ?  AND year = ?  ", level, Year-2).Count(&c)
	global.GVA_DB.Model(&Patent{}).Where("user_id = ?", userid).Where("patent_category = ?  AND year = ?  ", level, Year-3).Count(&d)
	global.GVA_DB.Model(&Patent{}).Where("user_id = ?", userid).Where("patent_category = ?  AND year = ?  ", level, Year-4).Count(&e)
	global.GVA_DB.Model(&Patent{}).Where("user_id = ?", userid).Where("patent_category= ?  AND year = ?  ", level, Year-5).Count(&f)
	data = []int64{f, e, d, c, b, a}
	return data
}

type ProjectMoney struct {
	ProjectMoney            float64
	ProjectTotalMoney       float64
	ProjectAccumulatedFunds float64
}

func GetAllPJMoney(year int, level string) ([]float64, int64) {
	var tt int64
	var a, t, r float64
	var data []Project
	global.GVA_DB.Where("project_level = ?", level).Find(&data).Count(&tt)

	var records []AnnualProjectRecord
	for _, pj := range data {
		global.GVA_DB.Where("project_id = ? and year = ?", pj.ID, year).Find(&records).Count(&tt)
		for _, record := range records {
			a += record.ProjectAccumulatedFunds
			r += record.ProjectMoney
		}
		t += pj.ProjectTotalMoney
	}
	moneny := []float64{r, t, a}
	return moneny, tt
}

func GetAllPJMoneyPerson(userid uint, year int, level string) ([]float64, int64) {
	var tt int64
	var a, t, r float64
	var data []Project
	global.GVA_DB.Where("project_level = ?", level).Find(&data).Count(&tt)

	var records []AnnualProjectRecord
	for _, pj := range data {
		global.GVA_DB.Where("user_id = ?", userid).Where("project_id = ? and year = ?", pj.ID, year).Find(&records).Count(&tt)
		for _, record := range records {
			a += record.ProjectAccumulatedFunds
			r += record.ProjectMoney
		}
		t += pj.ProjectTotalMoney
	}
	moneny := []float64{r, t, a}
	return moneny, tt
}
